package gui.search;

import gui.showrecipe.ShowRecipe;
import gui.tables.SearchTable;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.ImageIcon;

import org.jdom.JDOMException;

import start.Start;
import xml.XMLRecipeReader;

/**
 * Panel zur Rezeptsuche.
 * Beinhaltet eine Uebersichtstabelle ueber die gesuchten Rezepte
 * außerdem einen Button zum oeffnen der gefundenen Rezepte.
 * @author Dennis Oeguetveren
 * 
 */
public class SearchPanel extends JPanel {
	/** ID zur Serialisierung */
	private static final long serialVersionUID = 1L;
	/** Textfeld zum Suchen eines Rezeptes */
	private JTextField textField; 
	/** Button zum oeffnen eines gefundenen Rezeptes */
	private JButton btnOpen;
	/** Button zum suchen eines Rezeptes */
	private JButton btnSuche;
	/** Tabelle fuer Suchpanel */
	private final static SearchTable table = new SearchTable();
	/** Auslesen von bereits gespeicherten XML Informationen */
	private static XMLRecipeReader xmlreader;
	/** JPanel fuer Rezeptsuche */
	private static JPanel panel;
	
	/**
	 * Konstruiert das Panel fuer die Rezeptsuche
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public SearchPanel() throws JDOMException, IOException {
		xmlreader = new XMLRecipeReader(Start.getRezeptexml());
	
		/*Layout einstellen*/
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{160, 0, 0};
		gridBagLayout.rowHeights = new int[]{253, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		/*JPanel fuer Rezeptsuche konstruieren*/
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Suche nach:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{30, 217, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JLabel lblName = new JLabel("Name: ");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(5, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 0;
		panel.add(lblName, gbc_lblName);
		
		/*Textfeld zum suchen eines Rezeptes*/
		textField = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(5, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		panel.add(textField, gbc_textField);
		textField.setColumns(10);
		textField.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent arg0) {
				String output = textField.getText();
				int key = arg0.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					try {
						table.updateFromXML(xmlreader, output);
						textField.setText("");
					} catch (IOException e) {
						e.printStackTrace();
					} catch (JDOMException e) {
						e.printStackTrace();
					} 
				}
			}
			@Override public void keyReleased(KeyEvent arg0) {}
			@Override public void keyTyped(KeyEvent arg0) {}
		});
		
		/*ScrollPane fuer Rezeptsuche konstruieren*/
		JPanel tablePanel = table.buildPanel();
		tablePanel.setBackground(UIManager.getColor("Button.background"));
		GridBagConstraints gbc_tablePanel = new GridBagConstraints();
		gbc_tablePanel.gridwidth = 4;
		gbc_tablePanel.insets = new Insets(10, 0, 5, 5);
		gbc_tablePanel.fill = GridBagConstraints.BOTH;
		gbc_tablePanel.gridx = 0;
		gbc_tablePanel.gridy = 1;
		panel.add(tablePanel, gbc_tablePanel);
		table.getTable().addMouseListener(new MouseListener() {
			@Override public void mouseClicked(MouseEvent arg0) {
				try {
					if(table.getTable().getSelectedRowCount()>1)
						btnOpen.setEnabled(false); 
					else
						btnOpen.setEnabled(true); 
					
					if (arg0.getButton()==MouseEvent.BUTTON1 
                	&&  arg0.getClickCount()==2 
                	&& !arg0.isConsumed()) {
                		arg0.consume();
                		new ShowRecipe(table.getTable().getSelectedRow(), 1);
					}
				} catch (JDOMException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			@Override public void mouseEntered(MouseEvent arg0) {}
			@Override public void mouseExited(MouseEvent arg0) {}
			@Override public void mousePressed(MouseEvent arg0) {}
			@Override public void mouseReleased(MouseEvent arg0) {}
		});
		table.getTable().addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent arg0) {}
			@Override public void keyReleased(KeyEvent arg0) {
				if(table.getTable().getSelectedRowCount()>1)
					btnOpen.setEnabled(false); 
				else
					btnOpen.setEnabled(true); 
			}
			@Override public void keyTyped(KeyEvent arg0) {}
			
		});
		
		/*Button zum suchen eines Rezeptes konstruieren*/
		btnSuche = new JButton("Suchen", new ImageIcon(Start.getImgs().getSearchSmall()));
		GridBagConstraints gbc_btnSuche = new GridBagConstraints();
		gbc_btnSuche.insets = new Insets(5, 0, 5, 0);
		gbc_btnSuche.gridx = 2;
		gbc_btnSuche.gridy = 0;
		panel.add(btnSuche, gbc_btnSuche);
		btnSuche.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent arg0) {
				String output = textField.getText();
				try {
					table.updateFromXML(xmlreader, output);
					textField.setText("");
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JDOMException e) {
					e.printStackTrace();
				}
			}
		});
		btnSuche.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent e) {
				try {
					String output = textField.getText();
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						table.updateFromXML(xmlreader, output);
						textField.setText("");
					}
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			@Override public void keyReleased(KeyEvent e) {}
			@Override public void keyTyped(KeyEvent e) {}
		});
		
		/*Button zum öffnen eines gefundenen Rezeptes konstruieren*/
		btnOpen = new JButton("Öffnen", new ImageIcon(Start.getImgs().getOpenSmall()));
		GridBagConstraints gbc_btnOpen = new GridBagConstraints();
		gbc_btnOpen.insets = new Insets(5, 0, 5, 0);
		gbc_btnOpen.gridx = 0;
		gbc_btnOpen.gridy = 2;
		btnOpen.setEnabled(false);
		panel.add(btnOpen, gbc_btnOpen);
		btnOpen.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent arg0) {
				try {
					new ShowRecipe(table.getTable().getSelectedRow(), 1);
				} catch (JDOMException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		btnOpen.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent e) {
				try {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) 
						new ShowRecipe(table.getTable().getSelectedRow(), 1);
				} catch (JDOMException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			@Override public void keyReleased(KeyEvent e) {}
			@Override public void keyTyped(KeyEvent e) {}
		});
	}

	/**
	 * Liefert die Tabelle der Suchergebnisse
	 * @return	Tabelle
	 */
	public static SearchTable getTable() {
		return table;
	}

}
